<?php
/**
 * 媒资管理
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 10:02
 */
namespace app\api\controller\homepage1;
use app\api\controller\common\ApiBase;
use think\Db;
class Uploads extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        $this->admin = session('admin');
        $this->model = model('Companymgr');
    }


    public function getList(){
        $page = $this->request->param('page');
        $keyword = $this->request->param('title');
        $cate = $this->request->param('cate');
        $page = max(1,intval($page));
        $pageSize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
        $param=" and s.is_delete=0";
        if (!empty($keyword)) {
            $param.=" and s.name like '%$keyword%' ";
        }
//        if(!empty($cate)){
//            $cRes=db('server_cate')->where(['is_delete'=>0,'id'=>$cate])->find();
//            if($cRes){
//                if($cRes['parent_id'] == 0){
//                    $param.=" and s.cate_id in (select id form crm_server_cate where parent_id=$cate) ";
//                }else{
//                    $param.=" and s.cate_id=$cate ";
//                }
//            }else{
//                $param.=" and s.cate_id=$cate ";
//            }
//
//        }
        $sql2="select s.*,c.title as cate_name from crm_jq_media as s ";
        $sql2.="left join crm_jq_base_cate as c on c.id=s.cate_id";
        $sql2.=" where 1=1 $param";
        $sql2.=" order by s.sort desc";
//        var_dump($sql2);die;
        $total1=Db::query($sql2);
        $sql2.=" limit ".($page-1)*$pageSize.",$pageSize";
        $list=Db::query($sql2);
        $total=count($total1); //st
        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'count'=>$total]]);
    }


    /**
     * 添加
     * @return \think\response\Json|void
     *  zs_question       zs_question_option
     *  zs_question_type
     */
    public function add(){
        $data['create_time']=time();
        $data['update_time']=time();
        $data['title']=$this->request->param('title');   // 1单选 2多选3  填空
        $data['sort']=$this->request->param('sort');   // 1单选 2多选3  填空
//        $data['cate_id']=$this->request->param('cate_id');
        $res=db('jq_media')->insert($data);
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }


    /**
     * 删除
     */
    public function del(){
        $id = $this->request->param('id');
        $r = db('jq_media')->where(['id'=>$id,'is_delete'=>0])->find();
        if(empty($r)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        $r = db('jq_media')->where(['id'=>$id])->update(['is_delete'=>1]);
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败']);
        }
        return json(['code'=>1,'msg'=>'操作成功']);

    }


    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function edit()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $data['update_time']=time();
        $data['title']=$this->request->param('title');   // 1单选 2多选3  填空
//        $data['cate_id']=$this->request->param('cate_id');   // 1单选 2多选3  填空
//        $data['status']=$this->request->param('status');
        $data['sort']=$this->request->param('sort');
        $where['id'] = $id;
        $r=db('jq_media')->where($where)->update($data);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }
        return json(['code' => 1, 'msg' => '操作成功']);
    }





}